﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Subsonic.Data;
using MichMan.Utilities.Requests;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Media;

namespace Subsonic.ViewModels
{
    public class AlbumViewModel : PivotItemViewModel
    {
        public AlbumViewModel(Child album)
        {
           Id = album.Id;
           Title = album.Title;
           // Artist = album.Artist;
           AlbumImage = SubsonicRequestContext.CoverArtUrl(album.CoverArt);
        }

        public override IRequest LoadItems()
        {
            GetMusicDirectoryRequest albumReq = new GetMusicDirectoryRequest(Id);
            albumReq.RunRequestCompleted += new RunRequestCompletedEventHandler(albumReq_RunRequestCompleted);
//            albumReq.RunRequestAsync(album);
            return albumReq;
        }

        void albumReq_RunRequestCompleted(object sender, RunRequestCompletedEventArgs e)
        {
            GetMusicDirectoryRequest albumReq = (GetMusicDirectoryRequest)sender;

            if (albumReq.Response == null || albumReq.RequestStatus != RequestStatus.Succeeded)
            {
                // TODO:  Display error
                return;
            }

            foreach (var song in albumReq.Response.Directory.Children)
            {
                if (String.IsNullOrEmpty(Artist))
                {
                    Artist = song.Artist;
                }
                if (String.IsNullOrEmpty(Album))
                {
                    Album = song.Album;
                }

                Items.Add(new ChildItem(song));
            }
        }

        private void SetTitle()
        {
            string title = Artist;
            if (!String.IsNullOrEmpty(Album))
            {
                title += " - " + Album;
            }
            Title = title;
        }

        private ImageSource _image = null;
        public ImageSource Image
        {
            get
            {
                if (this._image == null && this.AlbumImage != null)
                {
                    if (this.AlbumImage.StartsWith("http"))
                    {
                        this._image = new BitmapImage(new Uri(this.AlbumImage));
                    }
                }
                return this._image;
            }

            set
            {
                this._image = value;
                NotifyPropertyChanged("Image");
                //                this.SetProperty(ref this._image, value);
            }
        }

        private string albumImage = null;
        public string AlbumImage
        {
            get
            {
                return albumImage;
            }
            set
            {
                if (value != albumImage)
                {
                    albumImage = value;
                    NotifyPropertyChanged("AlbumImage");
                    NotifyPropertyChanged("Image");
                }
            }
        }

        private string album = null;
        public string Album
        {
            get
            {
                return album;
            }
            set
            {
                if (value != album)
                {
                    album = value;
                    NotifyPropertyChanged("Album");
                    SetTitle();
                }
            }
        }

        private string artist = null;
        public string Artist
        {
            get
            {
                return artist;
            }
            set
            {
                if (value != artist)
                {
                    artist = value;
                    NotifyPropertyChanged("Artist");
                    SetTitle();
                }
            }
        }
    }
}
